package ph.com.gs3.formalistics.global.utilities;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import ph.com.gs3.formalistics.model.valueobjects.business.document.DocumentStub;
import ph.com.gs3.formalistics.view.valueobjects.parsers.DateUtilities;

public class Sorter {

	public static List<DocumentStub> sortDocumentsForDownload(List<DocumentStub> rawDocuments) {

		List<DocumentStub> sortedDocumentStubs = new ArrayList<DocumentStub>();
		sortedDocumentStubs.addAll(rawDocuments);

		Collections.sort(sortedDocumentStubs, new Comparator<DocumentStub>() {

			@Override
			public int compare(DocumentStub stub1, DocumentStub stub2) {

				try {
					Date firstItemDate = DateUtilities.parseToServerDate(stub1.dateUpdated);
					Date secondItemDate = DateUtilities.parseToServerDate(stub2.dateUpdated);
					return firstItemDate.compareTo(secondItemDate);
				} catch (ParseException e) {
					return 0;
				}

			}

		});

		return sortedDocumentStubs;

	}

}
